#include "head.h"
#include "queue.h"
#include <arpa/inet.h>
#include <asm-generic/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/socket.h>

int init_tcp(int *socket_fd,char* port,char* ip){

    *socket_fd = socket(AF_INET,SOCK_STREAM,0);

    int reuse = 1;
    setsockopt(*socket_fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));

    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);
    
    int ret_bind = bind(*socket_fd,(struct sockaddr*)&sockaddr,sizeof(sockaddr));

    int ret_lis = listen(*socket_fd,10);

    return 0;
}


int init_status(status_t *status,int num){

    for(int i=0;i<num;i++){

        status->thread_ids = (pthread_t*)malloc(sizeof(pthread_t)*num);
        status->thread_num = num;
        memset(&status->queue,0,sizeof(queue_t));

        pthread_mutex_init(&status->lock,NULL);
        pthread_cond_init(&status->cond,NULL);

        status->exit_flag = 0;
    }
    return 0;

}

int init_thread_pool(status_t *status){

    for(int i=0;i<status->thread_num;i++){
        pthread_create(&status->thread_ids[i],NULL,thread_main,status);
    }

    return 0;
}















